// index.js goods相关接口
const express = require('express')
const mysql = require('mysql')
// 创建路由对象
const goods = express.Router()
// 引入数据库配置文件
const database = require('../database')
// 创建数据库的连接对象
const db = mysql.createPool(database)
goods.use(express.json())
// 解析 application/x-www-form-urlencoded
goods.use(express.urlencoded({ extended: false }));
//商品信息列表接口
goods.get('/list', (req, res) => {
    let sql = "select * from t_goods"
    // 通过数据库对象访问数据库
    db.query(sql, [], (err, list) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
// 商品详情数据接口
goods.get('/getGoods', (req, res) => {
    let sql = "select * from t_goods where id=?"
    db.query(sql, [req.query.id], (err, goods) => {
        if (goods.length) {
            res.send({
                success: true,
                msg: '成功',
                goods: goods[0]
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
// 添加商品接口
goods.post('/addGoods', (req, res) => {
    let sql = 'INSERT INTO t_goods (title,content,imagePath,price) VALUES (?,?,?,?)'
    db.query(sql, [req.body.title, req.body.content, req.body.imagePath, req.body.price], (err, result) => {
        if (result.insertId) {
            res.send({
                "success": true,
                "msg": "成功"
            })
        } else {
            res.send({
                "success": false,
                "msg": "失败"
            })
        }
    })
})
// 修改商品接口
goods.post('/updateGoods',(req,res)=>{
    let sql='UPDATE t_goods SET title=?,content=?,imagePath=?,price=? where id=?'
    db.query(sql,[req.body.title, req.body.content, req.body.imagePath,
         req.body.price,req.body.id],(err,result)=>{
             if(err){
                 res.status(500).send("网络错误")
             }else{
                 if(result.changedRows){
                     res.send({
                        "success": true,
                        "msg": "成功"
                    })
                 }else{
                     res.send({
                        "success": false,
                        "msg": "失败"
                    })
                 }
             }
         })
})
goods.post('/comments',(req,res)=>{
    let sql='update t_comments set foodId=?,userId=?,comment=?,postdate=? where id=?'
    db.query(sql,[req.body.foodId,req.body.userId,req.body.comment,req.body.postdate,req.body.id],(err,result)=>{
        if(err){
            res.send(err)
        }else{
            if(result.changedRows){
                res.send({
                   "success": true,
                   "msg": "成功"
               })
            }else{
                res.send({
                   "success": false,
                   "msg": "失败"
               })
            }
        }
    })
})
//删除商品接口
goods.post('/deleteGoods',(req,res)=>{
    let sql='delete from t_goods where id=?'
    db.query(sql,[req.body.id],(err,result)=>{
        if(err||!result.affectedRows){
            res.send({
                "success": false,
                "msg": "失败"
            })
        }else{
            res.send({
                "success": true,
                "msg": "成功"
            })
        }
    })
})
module.exports = goods